<template>
  <div class="note-detail-container">
    <home-header />
    <div class="note-detail-content">
      <div class="note-detail-card">
        <div class="note-header">
          <h1 class="note-title">{{ note.title }}</h1>
          <div class="note-meta">
            <div class="note-tags">
              <el-tag v-for="tag in note.tags" 
                      :key="tag" 
                      size="small" 
                      class="note-tag">
                {{ tag }}
              </el-tag>
            </div>
            <span class="note-date">{{ formatDate(note.date) }}</span>
          </div>
        </div>
        <div class="note-content" v-html="note.content"></div>
      </div>
    </div>
  </div>
</template>

<script>
import HomeHeader from '@/components/HomeHeader.vue'

export default {
  name: 'NoteDetailView',
  components: {
    HomeHeader
  },
  data() {
    return {
      note: {
        id: null,
        title: '',
        content: '',
        tags: [],
        date: null
      }
    }
  },
  created() {
    this.loadNote()
  },
  methods: {
    loadNote() {
      const noteId = this.$route.query.id
      if (!noteId) {
        this.$message.error('笔记ID不存在')
        this.$router.push('/')
        return
      }

      const savedNotes = localStorage.getItem('studyNotes')
      if (savedNotes) {
        const notes = JSON.parse(savedNotes)
        const note = notes.find(n => n.id === parseInt(noteId))
        if (note) {
          this.note = note
        } else {
          this.$message.error('笔记不存在')
          this.$router.push('/')
        }
      } else {
        this.$message.error('笔记不存在')
        this.$router.push('/')
      }
    },
    formatDate(date) {
      const d = new Date(date)
      const pad = n => n.toString().padStart(2, '0')
      return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
    }
  }
}
</script>

<style scoped>
.note-detail-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f7f8fa 0%, #eef2f7 100%);
}

.note-detail-content {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 110px;
  min-height: 80vh;
}

.note-detail-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
  padding: 40px 36px 32px 36px;
  width: 900px;
  max-width: 96vw;
}

.note-header {
  margin-bottom: 32px;
  border-bottom: 1px solid #f0f2f5;
  padding-bottom: 24px;
}

.note-title {
  font-size: 2rem;
  font-weight: 700;
  color: #222;
  margin-bottom: 16px;
}

.note-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.note-date {
  color: #999;
  font-size: 0.9rem;
}

.note-tags {
  display: flex;
  gap: 8px;
}

.note-tag {
  margin-right: 8px;
}

.note-content {
  font-size: 1.1rem;
  line-height: 1.8;
  color: #333;
}

.note-content :deep(img) {
  max-width: 100%;
  height: auto;
  margin: 16px 0;
}

.note-content :deep(p) {
  margin: 16px 0;
}

.note-content :deep(h1),
.note-content :deep(h2),
.note-content :deep(h3),
.note-content :deep(h4),
.note-content :deep(h5),
.note-content :deep(h6) {
  margin: 24px 0 16px;
  color: #222;
  font-weight: 600;
}

.note-content :deep(ul),
.note-content :deep(ol) {
  padding-left: 24px;
  margin: 16px 0;
}

.note-content :deep(li) {
  margin: 8px 0;
}

.note-content :deep(blockquote) {
  border-left: 4px solid #409eff;
  margin: 16px 0;
  padding: 8px 16px;
  background: #f8fafc;
  color: #666;
}

.note-content :deep(code) {
  background: #f8fafc;
  padding: 2px 6px;
  border-radius: 4px;
  color: #409eff;
  font-family: monospace;
}

.note-content :deep(pre) {
  background: #f8fafc;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 16px 0;
}

.note-content :deep(pre code) {
  background: none;
  padding: 0;
  color: #333;
}

@media (max-width: 768px) {
  .note-detail-card {
    padding: 24px 20px;
  }
  
  .note-title {
    font-size: 1.6rem;
  }
  
  .note-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .note-date {
    margin-top: 8px;
  }
}
</style> 